package com.pw.lit.controller;

import com.pw.lit.bean.LearnData;
import com.pw.lit.bean.LearnDataExample;
import com.pw.lit.bean.LearnDirectory;
import com.pw.lit.bean.LearnDirectoryExample;
import com.pw.lit.service.LearnDataService;
import com.pw.lit.service.LearnDirectoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;


@Controller
@RequestMapping("/Learn")
public class LearnController {

    @Autowired
    private LearnDataService learnDataService;

    @Autowired
    private LearnDirectoryService learnDirectoryService;

    @RequestMapping("/learnindex")
    public String diaryindex(Model model, HttpSession session) {
        LearnDataExample example = new LearnDataExample();
        LearnDataExample.Criteria criteria = example.createCriteria();
        criteria.andLidEqualTo(1);
        List<LearnData> ldlists = learnDataService.selectByExample(example);
        LearnDirectoryExample example1 = new LearnDirectoryExample();
        LearnDirectoryExample.Criteria criteria1 = example1.createCriteria();
        criteria1.andLidIsNotNull();
        List<LearnDirectory> Type = learnDirectoryService.selectByExample(example1);

        session.setAttribute("count", ldlists.size());
        model.addAttribute("type", Type);
        System.out.println("学习首页");
//        model.addAttribute("learnDataLists",Lists);
        model.addAttribute("learnDataLists", pages(ldlists, 12, 1));
        return "FrontPages/learn/learn-index";
    }


    //获取分页数据
    @RequestMapping("/getdata")
    @ResponseBody
    public <E> List pagedata(Model model, @RequestParam("start") String start,
                             @RequestParam("limit") String limit,
                             @RequestParam("type") String type) {
        LearnDataExample example = new LearnDataExample();
        LearnDataExample.Criteria criteria = example.createCriteria();
        criteria.andLidEqualTo(Integer.parseInt(type));
        List<LearnData> Lists = learnDataService.selectByExample(example);
        List<LearnData> li = pages(Lists, Integer.parseInt(limit), Integer.parseInt(start) * 12 - 11);
        model.addAttribute("learnDataLists", li);
        return li;
    }

    //分页方法
    public <E> List<E> pages(List<E> list, int limit, int start) {
        List<E> nowList = new ArrayList<E>();
        if (limit + start <= list.size()) {
            for (int i = start - 1; i < start + limit - 1; i++) {
                nowList.add(list.get(i));
            }
        } else {
            for (int i = start - 1; i < list.size(); i++) {
                nowList.add(list.get(i));
            }
        }
        return nowList;
    }

    @RequestMapping("/count")
    @ResponseBody
    public int count(@RequestParam("type") String type) {
        LearnDataExample example = new LearnDataExample();
        LearnDataExample.Criteria criteria = example.createCriteria();
        criteria.andLidEqualTo(Integer.parseInt(type));
        return learnDataService.countByExample(example);
    }

    //进入学习资料详情页面
    @RequestMapping("/detail/{type}/{id}")
    public String count(Model model,
                        @PathVariable("type") String type,
                        @PathVariable("id") String id) {
        LearnDataExample example = new LearnDataExample();
        LearnDataExample.Criteria criteria = example.createCriteria();
        criteria.andLidEqualTo(Integer.parseInt(type));
        List<LearnData> dlist = learnDataService.selectByExample(example);
        model.addAttribute("dlist", dlist);
        model.addAttribute("learndata", selectbyid(id));
        return "FrontPages/learn/learn-detail";
    }

    //通过标签切换类型
    @RequestMapping("/changeTitle/{type}")
    public String changetitle(Model model,
                              @PathVariable("type") String type) {
        LearnDataExample example = new LearnDataExample();
        LearnDataExample.Criteria criteria = example.createCriteria();
        criteria.andLidEqualTo(Integer.parseInt(type));
        List<LearnData> dlist = learnDataService.selectByExample(example);
        if (!dlist.isEmpty()) {
            model.addAttribute("dlist", dlist);
            model.addAttribute("learndata", dlist.get(0));
        }
        return "FrontPages/learn/learn-detail";
    }

    @ResponseBody
    @RequestMapping("/selectdetail")
    public LearnData selectdetail(@RequestParam("id") String id) {
        return selectbyid(id);
    }

    @RequestMapping("/selectbytitle")
    @ResponseBody
    public LearnData selectbytitle(@RequestParam("id") String id) {
        return selectbyid(id);

//        LearnDataExample example = new LearnDataExample();
//        LearnDataExample.Criteria criteria = example.createCriteria();
//        criteria.andLidEqualTo(Integer.parseInt(type));
//        List<LearnData> dlist = learnDataService.selectByExample(example);
//        if (!dlist.isEmpty()) {
//            System.out.println(dlist.contains(learnDataService.selectByPrimaryKey(Integer.parseInt(id))));
//            System.out.println(dlist.indexOf(learnDataService.selectByPrimaryKey(Integer.parseInt(id))));
//            if(dlist.contains(learnDataService.selectByPrimaryKey(Integer.parseInt(id)))){
//                int i=dlist.indexOf(learnDataService.selectByPrimaryKey(Integer.parseInt(id)));
//                if(near.equals("pre")){
//                    model.addAttribute("learndata", dlist.get(i-1));
//                    return dlist.get(i-1);
//                }else if(near.equals("next")){
//                    model.addAttribute("learndata", dlist.get(i+1));
//                    return dlist.get(i+1);
//                }
//            }
//            model.addAttribute("dlist", dlist);
//        }
//        return learnDataService.selectByPrimaryKey(Integer.parseInt(id));
//        return "FrontPages/learn/learn-detail";
    }

    public LearnData selectbyid(String id) {
        return learnDataService.selectByPrimaryKey(Integer.parseInt(id));
    }
}
